{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# azure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "# 1、create  列表\n",
    "# faceListId\n",
    "faceListId = \"myboyfriends\" #学生填写\n",
    "#create_facelists_url = 'https://qdtapi.cognitiveservices.azure.com/face/v1.0/facelists/{}' #学生填写\n",
    "create_facelists_url = 'https://api-jimmy.cognitiveservices.azure.com/face/v1.0/facelists/{}'\n",
    "subscription_key = '0506b6e2cd2041d88e0993544d600153'#学生填写\n",
    "assert subscription_key\n",
    "\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "data = {\n",
    "        \"name\": \"my boyfriends\",\n",
    "        \"userData\": \"照片库\",\n",
    "        \"recognitionModel\": \"recognition_03\",\n",
    "# 学生填写\n",
    "    \n",
    "}\n",
    "\n",
    "r_create = requests.put(create_facelists_url.format(faceListId),headers=headers,json=data) #学生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"error\":{\"code\":\"FaceListExists\",\"message\":\"Face list \\'myboyfriends\\' already exists.\"}}'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "#先加一张脸试试\n",
    "# 2、Add face\n",
    "add_face_url = \"https://api-jimmy.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces\"#学生填写\n",
    "\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "img_url = \"https://gitee.com/ljwennn/api-job/raw/master/image/wulei.jpg\" #学生填写\n",
    "\n",
    "params_add_face={\n",
    "    \"userData\":\"吴磊\"\n",
    "#学生填写\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})#学生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaceId': '1beb5779-78e9-44ec-867e-719cb28eed0c'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.json()#返回persistedFaceId"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数方便添加图片\n",
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url =\"https://api-jimmy.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "\n",
    "    params_add_face={\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code#返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"https://gitee.com/ljwennn/api-job/raw/master/image/limingde.jpg\",\"李明徳\")\n",
    "AddFace(\"https://gitee.com/ljwennn/api-job/raw/master/image/zhangxincheng.jpg\",\"张新成\")\n",
    "AddFace(\"https://gitee.com/ljwennn/api-job/raw/master/image/liangchaowei.jpg\",\"梁朝伟\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '6a0c9324-a1b9-4b12-a642-c56a04a62b3c',\n",
       "  'faceRectangle': {'top': 102, 'left': 230, 'width': 187, 'height': 187},\n",
       "  'faceAttributes': {'smile': 1.0,\n",
       "   'headPose': {'pitch': -1.2, 'roll': -3.7, 'yaw': -0.1},\n",
       "   'gender': 'male',\n",
       "   'age': 23.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 1.0,\n",
       "    'neutral': 0.0,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'medium', 'value': 0.41},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.6},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.21},\n",
       "   'makeup': {'eyeMakeup': True, 'lipMakeup': True},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.12,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'black', 'confidence': 1.0},\n",
       "     {'color': 'gray', 'confidence': 0.65},\n",
       "     {'color': 'other', 'confidence': 0.61},\n",
       "     {'color': 'brown', 'confidence': 0.33},\n",
       "     {'color': 'blond', 'confidence': 0.04},\n",
       "     {'color': 'red', 'confidence': 0.01},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3.检测人脸\n",
    "face_api_url = 'https://api-jimmy.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "image_url = 'https://gitee.com/ljwennn/api-job/raw/master/image/liyuangen.jpg'\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择模型\n",
    "    'recognitionModel':'recognition_03',#此参数需与facelist参数一致\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数，请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "response = requests.post(face_api_url, params=params,headers=headers, json={\"url\": image_url})\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '4c529512-3930-4f79-8b9f-22c894784cf6',\n",
       "   'userData': '吴磊'},\n",
       "  {'persistedFaceId': '8f6f5cd1-1e0d-4337-9433-ddcc0738bd2b',\n",
       "   'userData': '李明徳'},\n",
       "  {'persistedFaceId': '355c0cf1-494f-4f9e-b32e-a9a43eb47a9e',\n",
       "   'userData': '张新成'},\n",
       "  {'persistedFaceId': 'c590df78-8ee6-4d6c-ac5c-3f2eb6308cf4',\n",
       "   'userData': '梁朝伟'},\n",
       "  {'persistedFaceId': 'd73be6c0-261d-498f-b440-564c84ddbeab',\n",
       "   'userData': '吴磊'},\n",
       "  {'persistedFaceId': 'cf6b9571-5590-4562-9e75-044163983c03',\n",
       "   'userData': '李明徳'},\n",
       "  {'persistedFaceId': '7d41ffec-d843-4d82-ae4f-d0d54c92535a',\n",
       "   'userData': '张新成'},\n",
       "  {'persistedFaceId': '0c21c832-07e9-45bd-bfed-4e3e738c6d0b',\n",
       "   'userData': '梁朝伟'}],\n",
       " 'faceListId': 'jianan8130',\n",
       " 'name': 'wulei_sqdd',\n",
       " 'userData': '照片库'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get facelist\n",
    "get_facelist_url =\"https://api-jimmy.cognitiveservices.azure.com/face/v1.0/facelists/{}\"#学生填写\n",
    "r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers,params=data)#学生填写\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'persistedFaceId': '568e0954-6042-4aa9-8d5f-e1e523c343c5',\n",
       "  'confidence': 0.2002},\n",
       " {'persistedFaceId': '1beb5779-78e9-44ec-867e-719cb28eed0c',\n",
       "  'confidence': 0.1196},\n",
       " {'persistedFaceId': '4ebb6ff4-f36e-42a2-9f0a-484734501e26',\n",
       "  'confidence': 0.09006},\n",
       " {'persistedFaceId': '1e704cc8-d2bc-427c-b6df-6b63d200c77d',\n",
       "  'confidence': 0.08999}]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.返回人脸相似置信度\n",
    "findsimilars_url = \"https://api-jimmy.cognitiveservices.azure.com/face/v1.0/findsimilars\"\n",
    "# 请求正文 faceID需要先检测一张照片获取\n",
    "data_findsimilars = {\n",
    "    \"faceId\":\"6a0c9324-a1b9-4b12-a642-c56a04a62b3c\", #取上方的faceID\n",
    "    \"faceListId\": \"myboyfriends\",\n",
    "    \"maxNumOfCandidatesRetlist_00urned\": 10,\n",
    "    \"mode\": \"matchFace\" #matchPerson #一种为验证模式，一种为相似值模式\n",
    "    }\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)\n",
    "r_findsimilars.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '1beb5779-78e9-44ec-867e-719cb28eed0c',\n",
       "   'userData': '吴磊'},\n",
       "  {'persistedFaceId': '4ebb6ff4-f36e-42a2-9f0a-484734501e26',\n",
       "   'userData': '李明徳'},\n",
       "  {'persistedFaceId': '568e0954-6042-4aa9-8d5f-e1e523c343c5',\n",
       "   'userData': '张新成'},\n",
       "  {'persistedFaceId': '1e704cc8-d2bc-427c-b6df-6b63d200c77d',\n",
       "   'userData': '梁朝伟'}],\n",
       " 'faceListId': 'myboyfriends',\n",
       " 'name': 'my boyfriends',\n",
       " 'userData': '照片库'}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看列表\n",
    "get_facelist_url = \"https://api-jimmy.cognitiveservices.azure.com/face/v1.0/facelists/{}\"\n",
    "r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers)\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1beb5779-78e9-44ec-867e-719cb28eed0c</td>\n",
       "      <td>吴磊</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4ebb6ff4-f36e-42a2-9f0a-484734501e26</td>\n",
       "      <td>李明徳</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>568e0954-6042-4aa9-8d5f-e1e523c343c5</td>\n",
       "      <td>张新成</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1e704cc8-d2bc-427c-b6df-6b63d200c77d</td>\n",
       "      <td>梁朝伟</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId userData\n",
       "0  1beb5779-78e9-44ec-867e-719cb28eed0c       吴磊\n",
       "1  4ebb6ff4-f36e-42a2-9f0a-484734501e26      李明徳\n",
       "2  568e0954-6042-4aa9-8d5f-e1e523c343c5      张新成\n",
       "3  1e704cc8-d2bc-427c-b6df-6b63d200c77d      梁朝伟"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用Pandas简化数据\n",
    "import pandas as pd\n",
    "# 返回facelist数据\n",
    "adf = pd.json_normalize(r_get_facelist.json()[\"persistedFaces\"])\n",
    "adf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>568e0954-6042-4aa9-8d5f-e1e523c343c5</td>\n",
       "      <td>0.20020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1beb5779-78e9-44ec-867e-719cb28eed0c</td>\n",
       "      <td>0.11960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4ebb6ff4-f36e-42a2-9f0a-484734501e26</td>\n",
       "      <td>0.09006</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1e704cc8-d2bc-427c-b6df-6b63d200c77d</td>\n",
       "      <td>0.08999</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId  confidence\n",
       "0  568e0954-6042-4aa9-8d5f-e1e523c343c5     0.20020\n",
       "1  1beb5779-78e9-44ec-867e-719cb28eed0c     0.11960\n",
       "2  4ebb6ff4-f36e-42a2-9f0a-484734501e26     0.09006\n",
       "3  1e704cc8-d2bc-427c-b6df-6b63d200c77d     0.08999"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回相似度数据\n",
    "bdf = pd.json_normalize(r_findsimilars.json())# 升级pandas才能运行\n",
    "bdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>568e0954-6042-4aa9-8d5f-e1e523c343c5</td>\n",
       "      <td>张新成</td>\n",
       "      <td>0.20020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1beb5779-78e9-44ec-867e-719cb28eed0c</td>\n",
       "      <td>吴磊</td>\n",
       "      <td>0.11960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4ebb6ff4-f36e-42a2-9f0a-484734501e26</td>\n",
       "      <td>李明徳</td>\n",
       "      <td>0.09006</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1e704cc8-d2bc-427c-b6df-6b63d200c77d</td>\n",
       "      <td>梁朝伟</td>\n",
       "      <td>0.08999</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId userData  confidence\n",
       "2  568e0954-6042-4aa9-8d5f-e1e523c343c5      张新成     0.20020\n",
       "0  1beb5779-78e9-44ec-867e-719cb28eed0c       吴磊     0.11960\n",
       "1  4ebb6ff4-f36e-42a2-9f0a-484734501e26      李明徳     0.09006\n",
       "3  1e704cc8-d2bc-427c-b6df-6b63d200c77d      梁朝伟     0.08999"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#合并\n",
    "pd.merge(adf, bdf,how='inner', on='persistedFaceId').sort_values(by=\"confidence\",ascending = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Face++"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "api_secret = '6w_OqzKm3NrKYA9y34_ojxpnSdc3vntz'\n",
    "api_key =  'KDKmcYsLUL0yfAIZscmjQVCuiXlQUGUZ'  # Replace with a valid Subscription Key here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. FaceSet Create\n",
    "import requests,json\n",
    "\n",
    "display_name = \"myboyfriends\"\n",
    "outer_id = \"1023\"\n",
    "user_data = \"5人，张新成，吴磊，梁朝伟，李源根，\"\n",
    "\n",
    "CreateFace_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'display_name':display_name,\n",
    "    'outer_id':outer_id,\n",
    "    'user_data':user_data\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(CreateFace_Url, params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '29a5c13c73c4c77ed702fe32a3ea5c88',\n",
       " 'time_used': 161,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1603527952,cab736f9-92b8-4383-b183-64721ffffac4',\n",
       " 'outer_id': '1023',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "GetDetail_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'outer_id':outer_id,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(GetDetail_Url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '29a5c13c73c4c77ed702fe32a3ea5c88',\n",
       " 'tags': '',\n",
       " 'time_used': 78,\n",
       " 'user_data': '5人，张新成，吴磊，梁朝伟，李源根，',\n",
       " 'display_name': 'myboyfriends',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603528013,63552697-0d45-4378-9cb8-c70891aa559f',\n",
       " 'outer_id': '1023'}"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "AddFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'29a5c13c73c4c77ed702fe32a3ea5c88',\n",
    "    'face_tokens':'b0407b9e803ebd39d511cd7956fd5bf5',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(AddFace_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '29a5c13c73c4c77ed702fe32a3ea5c88',\n",
       " 'time_used': 84,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1603528082,f8fc55e6-e4a9-4b42-a5a6-b03a98b1e215',\n",
       " 'outer_id': '1023',\n",
       " 'failure_detail': [{'reason': 'INVALID_FACE_TOKEN',\n",
       "   'face_token': 'b0407b9e803ebd39d511cd7956fd5bf5'}]}"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [],
   "source": [
    "Update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'b0407b9e803ebd39d511cd7956fd5bf5',\n",
    "    'user_data':\"5人，张新成，吴磊，梁朝伟，李源根，\",\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(Update_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 64,\n",
       " 'error_message': 'INVALID_FACESET_TOKEN',\n",
       " 'request_id': '1603528204,c0324ddc-bbc6-46ce-9ee1-536eaef938ea'}"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "lmd = \"https://gitee.com/ljwennn/api-job/raw/master/image/limingde.jpg\"\n",
    "zxc = \"https://gitee.com/ljwennn/api-job/raw/master/image/zhangxincheng.jpg\"\n",
    "lcw = \"https://gitee.com/ljwennn/api-job/raw/master/image/liangchaowei.jpg\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "Compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "payload ={\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'image_url1':lmd,\n",
    "    'image_url2':zxc,\n",
    "    'image_url3':lcw,\n",
    "    \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(Compare_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faces1': [{'face_rectangle': {'width': 129,\n",
       "    'top': 103,\n",
       "    'left': 282,\n",
       "    'height': 129},\n",
       "   'face_token': '9cef840dcc045d7eb62eb0616d8fa9a1'}],\n",
       " 'faces2': [{'face_rectangle': {'width': 264,\n",
       "    'top': 365,\n",
       "    'left': 328,\n",
       "    'height': 264},\n",
       "   'face_token': '5a7d256fac100db6e635a6c53a8c7f66'}],\n",
       " 'time_used': 2568,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'confidence': 40.156,\n",
       " 'image_id2': 'lVtIupNx6hMMMU6NZMbrIA==',\n",
       " 'image_id1': 'SgScwhNscE3QjldW55knGw==',\n",
       " 'request_id': '1603528377,5dec4eb2-7c51-43c7-b02b-5a90c85ad489'}"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Baidu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    " # encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=GoSyruicN07Y3fm7WkHfSxun&client_secret=ZFBPyxTpa2WozwjKPhgWWM1mG9Bo9uKc'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 9410175792510, 'timestamp': 1603530627, 'cached': 0, 'result': {'score': 0, 'face_list': [{'face_token': 'b9f93b3306689a30d13092095ec8f1bf'}, {'face_token': 'b7cd51fe75eb6fe1b9e0244720fa5acc'}]}}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "人脸对比\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/match\"\n",
    "\n",
    "params = \"[{\\\"image\\\": \\\"https://gitee.com/ljwennn/api-job/raw/master/image/zhaolusi.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"LIVE\\\", \\\"quality_control\\\": \\\"LOW\\\"},{\\\"image\\\": \\\"https://gitee.com/ljwennn/api-job/raw/master/image/liangchaowei.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"IDCARD\\\", \\\"quality_control\\\": \\\"LOW\\\"}]\"\n",
    "access_token ='24.449c9ca617876446b90319594d5f639d.2592000.1606121509.282335-22869214'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
